package max.ta.timer;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TimerManagerImpl
	implements TimerManager {

    private static final long PERIOD = 2 * 1000;
    private final Timer timer = new Timer();
    private final List<TimerTask> tasks = new ArrayList<TimerTask>();

    @Override
    public void addTask(TimerTask task) {
	// Check task state
	tasks.add(task);
    }

    @Override
    public void addAllTasks(List<TimerTask> tasks) {
	this.tasks.addAll(tasks);
    }

    @Override
    public void start() {
	Date now = Calendar.getInstance().getTime();
	for (TimerTask task : tasks) {

	    timer.scheduleAtFixedRate(task, now, PERIOD);
	}

    }

    @Override
    public void stop() {
	timer.cancel();
    }

}
